﻿using System;
using System.Runtime.Serialization;
using EzDesk.Core.Model;

namespace EzDesk.Apps.Model.Attendance
{
    /// <summary>
    /// 描述一天的出勤考勤情况
    /// </summary>
    [DataContract]
    public class DayAttendanceEvaluation : IModel
    {
        public DayAttendanceEvaluation()
        {
            this.Id = Guid.Empty;
        }

        [DataMember]
        public Guid UserId { get; set; }

        [DataMember]
        public Guid Id { get; set; }

        /// <summary>
        /// 日期
        /// </summary>
        [DataMember]
        public DateTime Date { get; set; }

        /// <summary>
        /// 是否需要工作
        /// </summary>
        [DataMember]
        public bool IsNeedToWork { get; set; }


        /// <summary>
        /// 是否迟到
        /// </summary>
        public bool IsLate
        {
            get
            {
                return LateMiniutes > 0;
            }
        }
        /// <summary>
        /// 迟到时间
        /// </summary>
        [DataMember]
        public int LateMiniutes { get; set; }


        /// <summary>
        /// 是否早退
        /// </summary>
        public bool IsLeaveEarly
        {
            get
            {
                return EarlyMiniutes > 0;
            }
        }
        /// <summary>
        /// 早退时间
        /// </summary>
        [DataMember]
        public int EarlyMiniutes { get; set; }

        /// <summary>
        /// 是否矿工
        /// </summary>
        [DataMember]
        public bool IsAbsense { get; set; }

        /// <summary>
        /// 是否加班
        /// </summary>
        [DataMember]
        public bool IsExtraWork { get; set; }

        /// <summary>
        /// 这一天实际获得的工时 (两位int类型表示 15 表示 1.5小时)
        /// </summary>
        [DataMember]
        public int Manhours { get; set; }

    }

    public static class DayEvaluationExtension
    {
        /// <summary>
        /// 将分钟为单位的工时转换为以小时为单位精确到0.5小时的工时
        /// </summary>
        /// <param name="miniutes"></param>
        /// <returns></returns>
        public static int ToManhours(this Int32 miniutes)
        {
            return miniutes / 6 / 5 * 5;
        }

        public static string GetEvaluationStatus(this DayAttendanceEvaluation source)
        {
            if (source.IsAbsense) return "旷工";

            if (source.IsExtraWork) return "加班";


            return (source.IsLate?"迟到":"") + (source.IsExtraWork?"早退":"");
        }
    }
}
